package DAO;

import Utils.GetStockInfo;
import Utils.Stock;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Repository
public class StockDAO
{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /*
    批量更新股票数据
 */
    public void UpdateBatchStock(ArrayList<Stock> stocks)
    {
       String sql = "insert into Stock values(?,?)";
       ArrayList<Object[]> objectList = new ArrayList<>();
       for(Stock s: stocks)
       {
           Object[] objects = new Object[]{s.getStockCode(),s.getStockName()};
           objectList.add(objects);
       }
       jdbcTemplate.batchUpdate(sql,objectList);
    }
    /*
    单个更新股票数据
     */
    public void updateSingleStock(Stock stock)
    {
        String sql = "insert into Stock values(?,?)";
        jdbcTemplate.update(sql,stock.getStockCode(),stock.getStockName());
    }
    /*
    查询全部股票数据
     */
    public ArrayList<Stock> GetAllStock()
    {
        String sql = "Select * from Stock";
        ArrayList<Stock> StockList;
        StockList = (ArrayList<Stock>) jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Stock.class));
        return StockList;
    }
    //根据股票代码查询股票信息
    public Stock GetStockByStockCode(String StockCode) throws Exception
    {
        String sql = "Select * from Stock Where StockCode = ?";
        Stock result = null;
        result = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Stock>(Stock.class),StockCode);
        return result;
    }

}
